package com.kzd.gtboon.main.mine

import android.view.LayoutInflater
import androidx.core.widget.addTextChangedListener
import androidx.lifecycle.LifecycleOwner
import androidx.recyclerview.widget.LinearLayoutManager
import com.blankj.utilcode.util.ClipboardUtils
import com.kzd.gtboon.R
import com.kzd.gtboon.custom.setOnClickAntiShakeListener
import com.kzd.gtboon.custom.setTransitionStatusE
import com.kzd.gtboon.databinding.ActivityMineGivingBinding
import com.kzd.gtboon.jetpack.vm.MineViewModel
import com.kzd.gtboon.main.mine.adapter.MineGiftAdapter
import com.kzd.gtboon.main.popup.GiftButtonPopup
import com.module.lemlin.app.OwnerStatusBar
import com.module.lemlin.http.Status
import com.module.lemlin.owner.OwnerViewBindingActivity
import com.module.lemlin.owner.ownerViewModel

class MineGiftActivity : OwnerViewBindingActivity<ActivityMineGivingBinding>() {

    private val mMineGiftAdapter = MineGiftAdapter()

    private var mAppSearchContent: String? = null

    private val mGiftButtonPopup: GiftButtonPopup by lazy {
        GiftButtonPopup(context = this)
    }

    private val mViewModel: MineViewModel by lazy {
        ownerViewModel(owner = this)
    }

    override val inflate: (LayoutInflater) -> ActivityMineGivingBinding
        get() = ActivityMineGivingBinding::inflate

    override fun initStatusBar(statusBar: OwnerStatusBar) {
        statusBar.run {
            addView = mViewBinding.flMineGiftBar
            barColor = R.color.white
        }
        super.initStatusBar(statusBar)
    }

    override fun initView() {

        mViewBinding.includeAppRefresh.rvAppRefreshBody.apply {
            layoutManager = LinearLayoutManager(context)
        }.adapter = mMineGiftAdapter

        mMineGiftAdapter.setOnItemClickListener { _, _, i ->
            val itemData = mMineGiftAdapter.getItem(i)
            mGiftButtonPopup.fillData(itemData)
                .showPopupWindow()
        }

        mMineGiftAdapter.setOnItemChildClickListener { _, view, position ->
            val itemData = mMineGiftAdapter.getItem(position)
            if (view.id == R.id.tvGameGiftCopy) {
                ClipboardUtils.copyText(itemData.card_no)
                toast("已复制${itemData.card_no}")
            }
        }

        mViewBinding.includeAppSearch.etAppSearchContent.addTextChangedListener {
            mAppSearchContent = it?.toString()
        }

        mViewBinding.includeAppSearch.tvAppSearchSubmit.setOnClickAntiShakeListener {
            mViewModel.mineGiftList(mAppSearchContent, isLoadMore = true)
        }

        mViewBinding.includeAppRefresh.mSmartRefreshLayout.setOnRefreshListener {
            mViewModel.mineGiftList(mAppSearchContent, isLoadMore = false)
        }
        mViewBinding.includeAppRefresh.mSmartRefreshLayout.setOnLoadMoreListener {
            mViewModel.mineGiftList(mAppSearchContent, isLoadMore = true)
        }
    }

    override fun initData() {
        mViewModel.mineGiftList()
    }

    override fun initViewMode() {

        mViewModel.giftListMyLiveData.observe(this as LifecycleOwner, { response ->
            setTransitionStatusE(response,
                mMineGiftAdapter,
                mViewBinding.includeAppRefresh.mSmartRefreshLayout)
            if (response.status != Status.SUCCESS)
                return@observe
            val data = response.data?.data
                ?: return@observe
            val isLoadMore = response.data?.isLoadMore
                ?: return@observe
            if (isLoadMore) {
                mMineGiftAdapter.addData(data)
            } else {
                mMineGiftAdapter.setNewInstance(data)
            }
        })
    }
}